/**
 *    Copyright 2009-2020 the original author or authors.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package org.apache.ibatis.aapl.driver;

import org.apache.ibatis.aapl.annotation.TableWatcher;
import org.apache.ibatis.aapl.reflect.MetaUtils;
import org.apache.ibatis.aapl.script.MapperEvaluator;
import org.apache.ibatis.aapl.script.SqlGenerator;
import org.apache.ibatis.aapl.util.StormAnnotationUtil;
import org.apache.ibatis.binding.MapperRegistry;
import org.apache.ibatis.executor.parameter.ParameterHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;
import org.apache.ibatis.parsing.XNode;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.scripting.xmltags.XMLLanguageDriver;
import org.apache.ibatis.session.Configuration;

import java.lang.reflect.Method;
import java.util.*;

/**
 * @author gu
 * @date 2020/5/20 9:53
 */
public class StormLanguageDriver extends XMLLanguageDriver {

  private Class<?> currentMapper;
  private TableWatcher currentWatcher;
  private int count = 0;
  private MapperEvaluator evaluator = new MapperEvaluator();
  private Map<String, List<Method>> methods;

  @Override
  public ParameterHandler createParameterHandler(MappedStatement mappedStatement, Object parameterObject, BoundSql boundSql) {
    return super.createParameterHandler(mappedStatement, parameterObject, boundSql);
  }

  @Override
  public SqlSource createSqlSource(Configuration configuration, XNode script, Class<?> parameterType) {
    return super.createSqlSource(configuration, script, parameterType);
  }

  @Override
  public SqlSource createSqlSource(Configuration configuration, String script, Class<?> parameterType) {
    MetaObject metaObject = MetaUtils.getMetaObject(configuration);
    MapperRegistry registry = (MapperRegistry) metaObject.getValue("mapperRegistry");
    Collection<Class<?>> mappers = registry.getMappers();
    if (mappers.size() > count) {
      Class<?> evaluate = evaluator.evaluate(mappers);
      currentMapper = evaluate;
      if (currentMapper == null) {
        currentWatcher = null;
      } else {
        currentWatcher = StormAnnotationUtil.getTableAnnotation(currentMapper);
      }
      methods = evaluator.scriptMethod(currentMapper);
      count = mappers.size();
    }

    Method method = methods.get(script).get(0);
    methods.get(script).remove(method);

    script = SqlGenerator.generateSql(script, method, currentWatcher);

    System.out.println(script);

    return super.createSqlSource(configuration, script, parameterType);
  }
}
